import { Component, OnInit } from '@angular/core';
import { HttpService } from '@core/services/http.service';
import swal from 'sweetalert2';
import { NzModalService } from 'ng-zorro-antd';
import { ModalComponent } from "./modal.component";

@Component({
    selector: 'app-widgets',
    templateUrl: './widgets.component.html',
    styleUrls: ['./widgets.component.less']
})
export class WidgetsComponent implements OnInit {

    _value;
    data = [];
    checkData = [];
    ps = 10;
    ps1 = 9999;
    pi = 1;
    pi1 = 1;
    loading = false;
    curRows = [];
    curRows1 = [];

    user_id = JSON.parse(localStorage.getItem('_user')).user_id;

    style: any = {
        width: '70%',
        top: '190px'
    };
    style1: any = {
        width: '40%',
        top: '190px'
    };
    ip = this.http.IP_PORT;

    allChecked = false;
    indeterminate = false;
    selectedRows = [];
    totalCallNo = 0;
    Total;

    allChecked1 = false;
    indeterminate1 = false;
    selectedRows1 = [];
    totalCallNo1 = 0;
    Total1;

    claId
    stuModal = false;
    classList = [];
    className = '';
    stuList = [];

    checkModal = false;

    classModal = false;
    className1;
    classCount;
    classData;
    key = '';
    constructor(public http: HttpService, public nzModal: NzModalService) {

    }

    showProTotal(id) {
        const subscription = this.nzModal.open({
            title: '已关联项目',
            content: ModalComponent,
            width: '70%',
            onOk() {
            },
            onCancel() {

            },
            footer: false,
            componentParams: {
                id: id
            }
        });
        subscription.subscribe(result => {
            if (result.start) {
                this.http.addSimulation({
                    cla_id: id,
                    p_id: result.data.p_id,
                    p_type: JSON.parse(result.data.p_type).value,
                    exercisetypes: 0,
                    deviceType: 'pc',
                    bigClass: 1,
                    claArr: [],
                    majArr: [],
                    day: 1
                }).subscribe(res => {

                });
            }
        });
    }

    checkAll(value: boolean) {
        this.curRows.forEach(i => {
            if (!i.disabled) i.checked = value;
        });
        this.refreshStatus();
    }

    refreshStatus() {
        const allChecked = this.curRows.every(value => value.disabled || value.checked);
        const allUnChecked = this.curRows.every(value => value.disabled || !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
        this.selectedRows = this.data.filter(value => value.checked);
        this.totalCallNo = this.selectedRows.reduce((total, cv) => total + cv.callNo, 0);
    }

    dataChange(res: any) {

        this.curRows = res;
        this.refreshStatus();
    }

    pageChange(pi: number) {

        this.pi = pi;
        this.loading = true;
        this.getData();
        this.loading = false;
    }

    checkAll1(value: boolean) {
        this.curRows1.forEach(i => {
            if (!i.disabled) i.checked = value;
        });
        this.refreshStatus1();
    }

    refreshStatus1() {
        const allChecked1 = this.curRows.every(value => value.disabled || value.checked);
        const allUnChecked1 = this.curRows.every(value => value.disabled || !value.checked);
        this.allChecked1 = allChecked1;
        this.indeterminate1 = (!allChecked1) && (!allUnChecked1);
        this.selectedRows1 = this.data.filter(value => value.checked);
        this.totalCallNo1 = this.selectedRows1.reduce((total, cv) => total + cv.callNo, 0);
    }

    dataChange1(res: any) {

        this.curRows1 = res;
        this.refreshStatus1();
    }

    pageChange1(pi: number) {

        this.pi1 = pi;
        this.loading = true;
        this.getData();
        this.loading = false;
    }


    doSearch() {
        this.key = this._value;
        this.getData();
    }

    ngOnInit() {
        this.getData();
    }

    getData() {
        this.loading = true;
        let param = {
            key: this.key,
            pi: '1',
            ps: '9999',
            tech_id: this.user_id
        };
        this.http.getClassListByTechId(param).subscribe(res => {
            this.loading = false;
            this.data = res['list'];
            this.Total = res['total'];
        });
    }

    showStuTotal(data) {
        //console.log(data);
        this.stuModal = true;
        this.claId = data.cla_id;
        this.className = data.cla_name;
        this.http.getStudentByClaId({ cla_id: data.cla_id }).subscribe(res => {
            this.stuList = res['list'];

        });
    }

    isDelStu(stu) {
        //console.log(stu);
        this.http.deleteStudent({ u_id: stu.Id }).subscribe(res => {
            //console.log(res);
            swal(res['msg'], '', 'info');
            if (res['code'] == 0) {
                this.http.getStudentByClaId({ cla_id: this.claId }).subscribe(res => {
                    //console.log(res);
                    this.stuList = res['list'];
                });
            }
        })
    }

    showCheckModal(data) {
        this.checkModal = true;
        this.http.getCheckStuList({ cla_id: data.cla_id }).subscribe(res => {
            this.checkData = res['list']
        })
    }

    through(data) {
        this.http.checkStudent({ check: 1, stu_id: data.stu_id }).subscribe(res => {
            if (res['code'] == 0) {
                this.showCheckModal(data)
                this.getData()
            }
            swal(res['msg'], '', 'info')
        })
    }

    reject(data) {
        this.http.checkStudent({ check: 0, stu_id: data.stu_id }).subscribe(res => {
            this.showCheckModal(data);
            if (res['code'] == 0) {
                this.showCheckModal(data)
                this.getData()
            }
            swal(res['msg'], '', 'info')
        })
    }

    edit(data) {
        this.classModal = true;
        this.classData = data;
        this.className1 = data.cla_name;
        this.classCount = data.count;
    }

    classNameChange($event) {
        this.className1 = $event
    }

    classCountChange($event) {
        this.classCount = $event
    }

    saveClass() {
        if (!this.className1 || !this.classCount) {
            swal('班级名称、人数不能为空', '', 'info')
            return
        }
        this.classData.cla_name = this.className1;
        this.classData.count = this.classCount;
        this.http.saveClass(this.classData).subscribe(res => {
            this.classModal = false
            swal(res['msg'], '', 'info')
        })

    }
}
